<!--
 * @Author: liujinjun681 liujinjun681@qq.com
 * @Date: 2022-09-08 16:04:23
 * @LastEditors: liujinjun681 liujinjun681@qq.com
 * @LastEditTime: 2022-11-21 08:43:08
 * @FilePath: \hd_ims_web\src\views\output\outputData\qrCode\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="24"></el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card>
          <div slot="header">
            <span>操作台</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="creatQr">生成二维码</el-button>
          </div>
          <el-form label-width="120px" :model="form" ref="formQr">
            <el-form-item label="班次" prop="rrqBc" required>
              <el-radio-group v-model="form.rrqBc" size="large" style="padding-left: 15px" @change="clearQr">
                <template v-if="$store.getters.isWuHan">
                  <el-radio label="白班-A" />
                  <el-radio label="白班-B" />
                  <el-radio label="夜班-A" />
                  <el-radio label="夜班-B" />
                  <el-radio label="部装" />
                  <el-radio label="返工入库" />
                </template>
                <template v-else>
                  <el-radio label="白班-甲" />
                  <el-radio label="白班-乙" />
                  <el-radio label="夜班-甲" />
                  <el-radio label="夜班-乙" />
                  <el-radio label="部装" />
                  <el-radio label="返工入库" />
                </template>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="生产日期" prop="rrqRq" required>
              <el-date-picker v-model="form.rrqRq" value-format="yyyy-MM-dd" @change="clearQr" />
            </el-form-item>
            <el-form-item label="机台编号" prop="rrqJtbh" :required="form.rrqBc !== '返工入库'">
              <el-select filterable clearable v-model="form.rrqJtbh" @change="clearQr">
                <el-option v-for="item in listJtmJtbh" :key="item" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="客户代码">
              <el-autocomplete
                type="text"
                :fetch-suggestions="getAllCustomer"
                v-model="customerCode"
                :trigger-on-focus="false"
                value-key="itmCustwldm"
                @select="FillAttribute" />
            </el-form-item>
            <el-form-item label="每箱数量" prop="rrqNumber" required>
              <el-autocomplete :fetch-suggestions="getBzsl" v-model="form.rrqNumber" clearable @change="clearQr" />
            </el-form-item>
            <el-form-item label="入库箱数" prop="count" required>
              <el-input-number
                size="large"
                v-model="form.count"
                controls-position="right"
                :precision="0"
                :min="1"
                :max="100"
                @change="clearQr"></el-input-number>
            </el-form-item>
            <el-form-item label="品名规格">
              <el-input size="large" disabled readonly v-model="pmgg"></el-input>
            </el-form-item>
            <el-form-item label="物料代码">
              <el-input size="large" disabled v-model="form.rrqWldm"></el-input>
            </el-form-item>
            <el-form-item label="入库客户">
              <el-input size="large" disabled readonly v-model="rkkh"></el-input>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <div slot="header">
            <span>二维码</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="printQr">打印二维码</el-button>
          </div>
          <div class="scrollbarQr" id="printContent">
            <div class="divqr" v-for="(item, index) in listQr" :key="item.rrqId">
              <div>
                <VueQr :text="mainKey + item.rrqId" :size="165" :correctLevel="3"></VueQr>
                <ol class="ollist">
                  <li>生产日期：{{ $moment(item.rrqRq).format('YYYY-MM-DD') }} &emsp;[{{ item.rrqJtbh }}]</li>
                  <li>
                    <font>客户代码：</font>
                    <font style="font-size: 20px">{{ customerCode }}</font>
                  </li>
                  <li>品名：{{ pm }}</li>
                  <li>班次：{{ item.rrqBc }}</li>
                  <li>数量：{{ item.rrqNumber }}</li>
                  <li>箱号：{{ item.rrqXs }}</li>
                </ol>
              </div>
              <div class="divrkz">入库章</div>
              <div class="divzjz">质检章</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import VueQr from 'vue-qr'
import printJS from 'print-js'
import { getAll } from '@/api/repertory/Storage'
import { generateQr, updatePrint } from '@/api/repertory/putstorage'
import { automaticStorage, PDACusWldmList, getItmMstrByPDACustwldm, getStatisticsWarehousing } from '@/api/repertory/PDA'

import mixins from '@/mixins/common'

export default {
  name: 'GenerateQr',
  mixins: [mixins.listJtmJtbh],
  components: {
    VueQr,
  },
  data() {
    return {
      form: {
        rrqBc: '白班-A',
        rrqRq: new Date(),
        rrqWldm: null,
        rrqNumber: '',
        count: '',
        rrqJtbh: '',
      },
      date: '',
      BcDm: '白班',
      customerCode: '',
      wldm: '',
      bzsl: '',
      pmgg: '',
      khdm: '',
      rkkh: '',
      rkck: '',
      pm: '',
      listQr: [],
      num: null,
      //提供的所有客户代码
      provider_list_data: [],
      //符合条件的客户代码
      provider_list: [],
      //入库仓库
      warehouse_list: [],
      //入库类型
      type_list: [],
      //入库客户
      kh_list: [],
      mainKey: this.$store.state.settings.sysName.indexOf('荆州') > -1 ? 'J2022-' : '2022-',
    }
  },
  created() {
    //请求所有客户代码
    PDACusWldmList().then((response) => {
      this.provider_list_data = response.data
    })

    //所有仓库
    getAll().then((response) => {
      this.warehouse_list = response.data
      for (let item of this.warehouse_list) {
        item.itemView = '[' + item.storageCode + ']' + item.storageName
        item.listClass = 'default'
        item.itemCode = item.storageId + ''
      }
    })
  },
  methods: {
    msg(msg, type) {
      this.$message({ showClose: true, message: msg, type: 'success' })
    },
    getBzsl(queryString, cb) {
      cb(
        this.bzsl.split(';').map((a) => {
          return { value: a }
        })
      )
    },
    printQr() {
      if (this.listQr.length == 0) {
        this.$message.warning('请先生成二维码！')
        return
      }
      updatePrint()
      setTimeout(() => {
        printJS({
          documentTitle: 'qr-' + this.customerCode,
          printable: 'printContent', // // 文档来源：pdf或图像的url，html元素的id或json数据的对象
          type: 'html', // 可打印的类型。可用的打印选项包括：pdf，html，图像，json和raw-html。
          style: ['@page { margin: 0mm;} body {margin: 0;} h4 {margin:0}'],
          targetStyles: ['*'], // 库在打印HTML元素时处理任何样式
        })
      }, 200)
    },
    creatQr() {
      this.$refs['formQr'].validate((valid) => {
        if (valid) {
          if (this.form.rrqWldm !== null && this.form.rrqWldm !== '') {
            this.listQr = []
            generateQr(this.form).then((res) => {
              this.listQr = res.data
              this.$message.success('二维码生成成功！')
            })
          } else {
            this.$message.warning('请确认该客户代码是否存在！')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearQr() {
      this.listQr = []
    },
    clearALL() {
      this.listQr = []
      this.date = ''
      this.wldm = ''
      this.pmgg = ''
      this.pm = ''
      this.rkkh = ''
      this.rkck = ''
      this.form.rrqWldm = ''
      this.num = null
      // this.handleFocus('start')
    },
    getAllCustomer(queryString, cb) {
      if (this.customerCode == null || this.customerCode == '') {
        return
      }
      let temp = []
      this.clearALL()
      temp = this.provider_list_data
        .filter((i) => {
          const a = i.itmCustwldm.match(/\d{4}(?!\d)/g)
          return a && a[a.length - 1].indexOf(this.customerCode) != -1
        })
        .slice(0, 30)
      cb(temp)
    },
    FillAttribute() {
      this.listQr = []
      if (this.customerCode == null || this.customerCode == '') {
        return
      }

      getItmMstrByPDACustwldm({ itmCustwldm: this.customerCode }).then((res) => {
        if (res.code == '200') {
          this.form.rrqWldm = res.data.itmWldm
          this.pmgg = res.data.itmPmgg
          this.rkkh = res.data.itmKhdm
          this.rkck = res.data.itmCkdm
          this.pm = res.data.itmWlpm
          this.bzsl = res.data.itmBzsl
          this.form.rrqNumber = null
          this.$Message.success('成功')
        } else {
          alert(res.msg)
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.app-container {
  ::v-deep .is-disabled .el-input__inner {
    background-color: #ffffff;
    color: #ff0000;
  }
}
.ollist {
  list-style-type: none;
  margin: 15px 0px;
  width: 240px;
  font-size: 17px;
  font-weight: 700;
  line-height: 21px;
  white-space: nowrap;
}
.divqr {
  height: 250px;
  width: 400px;
  page-break-inside: avoid;
  position: relative;
  > div {
    page-break-inside: avoid;
    display: flex;
    align-content: center;
    justify-content: center;
    align-items: center;
  }
  .divrkz {
    margin: 5px 20px;
    height: 50px;
    font-size: 22px;
    width: 200px;
    border: 2px dashed #000000;
  }
  .divzjz {
    position: absolute;
    border-radius: 100%;
    font-size: 22px;
    border: 2px dashed #000000;
    bottom: 30px;
    right: 10px;
    width: 110px;
    height: 110px;
  }
}
.scrollbarQr {
  // height: 500px;
  // overflow-y: auto;
  width: 400px;
  ::v-deep .el-scrollbar__wrap {
    overflow-x: hidden;
  }
  ::v-deep .is-horizontal {
    display: none;
  }
}
</style>
